#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Creates entropy contour map
'''
from __future__ import division

import matplotlib.pyplot

#import common
import os
import sys
import math
import scipy

#draw exact diagonalization picture
path = '../../J_1.0/B_3.0/N3/exact_diagonalisation/entropy'

result = []
for T in os.listdir(path):
    S = eval(open(os.path.join(path, T)).read())
    result += [ ( float(T.replace('T','')), S ) ]

result.sort()

T = [tx[0] for tx in result]
S = [tx[1] - math.log(2) / 9 for tx in result]

matplotlib.pyplot.plot(T, S, label = 'Exact diagonalization')

#draw picture as Richards equation

pathJ = '../../results/two_d_triang/J_1.0/B_3.0/N3/dT0.05/Energy_J'
pathB = '../../results/two_d_triang/J_1.0/B_3.0/N3/dT0.05/Energy_B'

result = []
for T in os.listdir(pathJ):
    EJ = eval(open(os.path.join(pathJ, T)).read())
    EB = eval(open(os.path.join(pathB, T)).read())
    result += [ ( float(T.replace('T','')), EJ + EB ) ]

result.sort()

T = [tx[0] for tx in result]
beta = [1 / tx[0] for tx in result]
E = [tx[1] for tx in result]

#path = '../../results/two_d_triang/J_1.0/B_3.0/N3/dT0.1/C'
path = '../../results/two_d_triang/J_1.0/B_0.0/N3/dT0.1/C'


#summation as Richards equation with summation as of T
#int_T_{\infty} E(T') / T'**2 dT' 

temp1 = []
for i in xrange(len(E)):
    temp1 += [E[i] / T[i]**2]

S1 = []

for i in xrange(len(result)):
    S1 += [ math.log(2)  + E[i] / T[i] - scipy.trapz(temp1[i:], T[i:]) ]    

#matplotlib.pyplot.plot(T, S1, "*", label = "$S = S(\infty) + E(T) /  T - \int_T^{\infty} E(T) / T^2 dT$")



   
S4 = []
for i in xrange(len(result)):
    S4 += [ math.log(2) + E[i] / T[i] + scipy.trapz(E[i:], beta[i:]) ]    

matplotlib.pyplot.plot(T, S4, "*", label = "$S = S(\infty) + E(T) / T - \int_{(1/T)}^{0} E(1/T) d (1/T)$")


#lets work with specific heat as with derivative
temp5_C = []
temp5_T = []

for i in xrange(len(E)-1):
    temp5_C += [ (E[i + 1] - E[i]) / ( (T[i + 1] - T[i]) * (T[i + 1] + T[i]) / 2 )]
    temp5_T += [ (T[i + 1] + T[i]) / 2 ]

S5 = []
for i in xrange(len(temp5_C)):
    S5 += [ scipy.trapz(temp5_C[:i], temp5_T[:i]) ]    

#matplotlib.pyplot.plot(temp5_T, S5, "*", label = "$S = \int_0^T C(T) / T dT$, $C$ from thermodynamics")

S6 = []
for i in xrange(len(temp5_C)):
    S6 += [ math.log(2)- scipy.trapz(temp5_C[i:], temp5_T[i:]) ]    

#matplotlib.pyplot.plot(temp5_T, S6, "*", label = "$S = S(\infty) - \int_T^{\infty} C(T) / T dT$")



matplotlib.pyplot.ylabel('Entropy')
matplotlib.pyplot.xlabel('Temperature')
matplotlib.pyplot.legend(loc = "upper left")
matplotlib.pyplot.show()
